from rest_framework import serializers
from . import models
class CourseCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = models.CourseCategory
        fields = ['id','name']


class TeacherSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Teacher
        fields = ['id', 'name', 'title', 'signature', 'image', 'brief', 'role_name']

class CourseSerializer(serializers.ModelSerializer):
    # 加一个老师的字段 三种方法
    # 方法一：SerializerMethodField
    # teacher = serializers.SerializerMethodField()
    # def get_teacher(self,obj):
    #     return{'name':obj.teacher.name}
    # 方法二：在models中重写一个方法
    # 方法三：子序列化--》老师的序列化类即可
    teacher = TeacherSerializer()
    class Meta:
        model = models.Course
        # 返回课时，最多返回4条，超过4条，也是4条，不足4条，有多少返回多少
        fields = ['id', 'name', 'students', 'teacher', 'sections', 'pub_sections', 'price', 'course_section_list',
                  'course_img','level_name']

class CourseSectionSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.CourseSection
        fields = ['id', 'name', 'section_type_name', 'section_link', 'duration', 'free_trail']

class CourseChapterSerializer(serializers.ModelSerializer):
    # 子序列化
    coursesections = CourseSectionSerializer(many=True)
    class Meta:
        model = models.CourseChapter
        fields = ['id', 'name', 'summary', 'pub_date', 'coursesections']
